package com.cy.pet.user.controller;

import com.cy.pet.common.pojo.User;
import com.cy.pet.common.service.UserService;
import com.cy.pet.common.vo.SysResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @description:
 * @author: LiuXingYu
 * @date: 2021/9/30 17:01
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @PostMapping("/login/{username}")
    public User getUserByName(@PathVariable("username") String username){
        return userService.getUserByUserName(username);
    }
    /*注册用户信息*/
    // @PreAuthorize("hasAuthority('sys:res:create')")
    @PostMapping("/register")
    public SysResult register(String username, String password, String repassword, String phone, String email){
        //判断两次密码输入是否一致
        if(!password.equals(repassword)) return SysResult.fail("两次输入密码不一致，请重新输入");
        User user = new User().setUsername(username).setEmail(email).setPassword(password).setStatus(0).setPhone(phone);
        //进入service层实现注册业务
        Boolean flag = userService.register(user);
        //如果返回false，说明用户名以被注册
        if(!flag) return SysResult.fail("用户名已注册，请重新登录");
        return SysResult.success();
    }

}
